﻿using IMES.Common.Domain.DurianModel;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class DefectiveSearchHander : IRequestHandler<DefectiveSearchCommand, ResultPage<Defectives>>
    {


        private readonly IBaseRepositorys<Defectives> baseRepositorys;
        private readonly IBaseRepositorys<SystemLogs> systemLogsRepositorys;
        public DefectiveSearchHander(IBaseRepositorys<Defectives> baseRepositorys, IBaseRepositorys<SystemLogs> systemLogsRepositorys)
        {
            this.baseRepositorys = baseRepositorys;
            this.systemLogsRepositorys = systemLogsRepositorys;
        }

        /// <summary>
        /// 查询显示不良品项信息
        /// </summary>
        /// <param name="request">查询不良品项信息请求对象</param>
        /// <param name="cancellationToken">允许异步</param>
        /// <returns>查询不良品项信息响应对象</returns>
        public async Task<ResultPage<Defectives>> Handle(DefectiveSearchCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var  result = new ResultPage<Defectives>();
                var query = baseRepositorys.GetAll().Where(x => !x.DeleteDate);
                if (!string.IsNullOrEmpty(request.DefectiveName))
                {
                    query = query.Where(x => x.DefectiveName.Contains(request.DefectiveName));
                }
                if (!string.IsNullOrEmpty(request.DefectiveNo))
                {
                    query = query.Where(x => x.DefectiveNo.Contains(request.DefectiveNo));
                }

                result.Data =  query.ToList();
                SystemLogs systemLogs = new SystemLogs();
                systemLogs.LogeName =  "查询了不良品项";
                systemLogs.LogeTime = DateTime.Now;
                systemLogsRepositorys.Add(systemLogs);
                return result;

            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
